package com.SFA.Customer;


import java.util.ArrayList;
import java.util.List;

import com.FrameWork.Common.AbstractBaseActivity;
import com.FrameWork.Common.MyApplication;
import com.FrameWork.Entity.CustomerInfoEntity;
import com.FrameWork.Utils.DateTimeUtils;
import com.FrameWork.Utils.Utils;
import com.SFA.R;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteCursor;
import android.database.sqlite.SQLiteStatement;

import com.umeng.analytics.MobclickAgent;

public class CustomerManager {

	private Activity				     owner;

	public CustomerManager(Activity activity)
	{
		this.owner = activity;
	}

	public CustomerManager(Context context) {
		// TODO Auto-generated constructor stub
		this.owner = (Activity)context;
	}

	private String	     TempHtmlPath = null;


	public List<CustomerInfoEntity> getCustomerList()
	{
		List<CustomerInfoEntity> list = new ArrayList<CustomerInfoEntity>();
		SQLiteCursor cursor = null;
		try
		{

			String sql = String.format(owner.getString(R.string.sql_GetCustomerList));
			cursor = MyApplication.getInstance().DataProvider .Query(sql);

			while (cursor.moveToNext())
			{
				CustomerInfoEntity entity = new CustomerInfoEntity();
				entity.ID = cursor.getString(cursor.getColumnIndex("ID"));
				entity.NAME = cursor.getString(cursor.getColumnIndex("NAME"));
				entity.ADDRESS_LINE = cursor.getString(cursor.getColumnIndex("ADDRESS_LINE"));
				entity.FAX = cursor.getString(cursor.getColumnIndex("FAX"));
				entity.CUSTOMER_PRODUCT_LIST_ID = cursor.getString(cursor.getColumnIndex("CUSTOMERPRODUCTLISTID"));
				list.add(entity);
			}
		}
		catch (Exception ex)
		{
			MobclickAgent.reportError(owner, ex.toString());
		}
		finally
		{
			if (cursor != null)
			{
				cursor.close();
				cursor = null;
			}
		}
		return list;
	}

	

	public CustomerInfoEntity getCustomerInfo(String customerId)
	{
		SQLiteCursor cursor = null;
		CustomerInfoEntity entity = new CustomerInfoEntity();
		try
		{
			String sql = String.format(owner.getString(R.string.sql_GetCustomerInfo),
					customerId);
			cursor = MyApplication.getInstance().DataProvider.Query(sql);

			if (cursor.moveToNext())
			{
				entity.ADDRESS_LINE = cursor.getString(cursor.getColumnIndex("ADDRESS_LINE"));
				entity.CODE = cursor.getString(cursor.getColumnIndex("CODE"));
				entity.CUST_CLASS_CODE= cursor.getString(cursor.getColumnIndex("CUSTCLASSCODE"));
				entity.CUSTOMER_PRODUCT_LIST_ID = cursor.getString(cursor.getColumnIndex("CUSTOMERPRODUCTLISTID"));
				entity.E_MAIL= cursor.getString(cursor.getColumnIndex("E_MAIL"));
				entity.FAX= cursor.getString(cursor.getColumnIndex("FAX"));
				entity.ID =customerId;
				entity.LONGITUDE = cursor.getString(cursor.getColumnIndex("LONGITUDE"));
				entity.LATITUDE= cursor.getString(cursor.getColumnIndex("LATITUDE"));
				entity.LOCAL_LEVEL1_CODE = cursor.getString(cursor.getColumnIndex("LOCALLEVEL1CODE"));
				entity.MEASURE_PROFILE_ID= cursor.getString(cursor.getColumnIndex("MEASUREPROFILEID"));
				entity.NAME= cursor.getString(cursor.getColumnIndex("NAME"));
				//entity.NAME2= cursor.getString(cursor.getColumnIndex("address_line"));
				entity.TELEPHONE= cursor.getString(cursor.getColumnIndex("TELEPHONE"));
			}
		}
		catch (Exception ex)
		{
			MobclickAgent.reportError(owner, ex.toString());
		}
		finally
		{
			if (cursor != null)
			{
				cursor.close();
				cursor = null;
			}
		}
		return entity;
	}

	public boolean save(CustomerInfoEntity entity, boolean upload)
	{

		Object[] values = new  Object[14];
		int i=0;
		if(entity.ID == null || entity.ID.length()==0)
		{
			entity.ID = Utils.getId(DateTimeUtils.GetCurrentTime());
		}
		values[i++] = entity.ID;
		values[i++] = entity.NAME;
		//values[2] = entity.NAME2;
		values[i++] = entity.MEASURE_PROFILE_ID;
		values[i++] = entity.CUSTOMER_PRODUCT_LIST_ID;
		values[i++] = entity.LONGITUDE;
		values[i++] = entity.LATITUDE;
		values[i++] = entity.CUST_CLASS_CODE;
		values[i++] = entity.LOCAL_LEVEL1_CODE;
		values[i++] = entity.LOCALLEVEL2CODE;
		values[i++] = entity.DEFAULTUOMCODE;
		values[i++] = entity.CHAINCODE;
		values[i++] = "True";
		values[i++] = entity.ID;
		values[i++] = "CS";

		Object[] values2 = new  Object[6];
		i=0;
		values2[i++] = entity.ID;
		values2[i++] = entity.ADDRESS_LINE;
		//values[2] = entity.NAME2;
		values2[i++] = entity.TELEPHONE;
		values2[i++] = entity.FAX;
		values2[i++] = entity.E_MAIL;
		values2[i++] = "True";
		try
		{
			MyApplication.getInstance().DataProvider.myDataBase.beginTransaction();
			//插入信息到客户资料表
			SQLiteStatement statement = MyApplication.getInstance().DataProvider.CompileStatement(owner.getResources().getString(R.string.sql_UpdateorInsertCustomerInfo));
			for(int l=0;l<values.length;l++)
			{
				statement.bindString(l+1, values[l].toString());
			}
			statement.execute();

			if(upload)
			{
				//插入到待上传表
				statement = MyApplication.getInstance().DataProvider.CompileStatement(owner.getResources().getString(R.string.sql_UpdateorInsertCustomerInfo2));
				for(int l=0;l<values.length;l++)
				{
					statement.bindString(l+1, values[l].toString());
				}
				statement.execute();
				//插入信息到上传联系信息表
				MyApplication.getInstance().DataProvider .Execute(owner.getResources().getString(R.string.sql_UpdateorInsertContactInfo2), values2);
			}
			MyApplication.getInstance().DataProvider .Execute(owner.getResources().getString(R.string.sql_UpdateorInsertContactInfo), values2);
			
			MyApplication.getInstance().DataProvider .myDataBase.setTransactionSuccessful();

		}
		catch(Exception err)
		{
			MobclickAgent.reportError(owner, err.getMessage());
			return false;
		}
		finally
		{
			if(MyApplication.getInstance().DataProvider  != null)
			{
				MyApplication.getInstance().DataProvider .myDataBase.endTransaction();

			}
		}
		return true;
	}
}
